using Sage.Http.Core;
using Sage.Http.Factory;
using Sage.Http.Models;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace Sage.Http.Extensions
{
    /// <summary>
    /// 为 HttpRequestManager 提供 JSON 操作扩展方法
    /// </summary>
    public static class HttpRequestManagerJsonExtensions
    {
        #region GetFromJsonAsync - 兼容性方法

        /// <summary>
        /// 发送 HTTP GET 请求并将响应内容反序列化为指定类型（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        public static Task<T?> GetFromJsonAsync<T>(
            this IHttpRequestManager manager,
            string requestUri,
            JsonTypeInfo<T> jsonTypeInfo,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            return GetFromJsonAsync(manager, requestUri, jsonTypeInfo, null, cancellationToken);
        }

        /// <summary>
        /// 发送 HTTP GET 请求并将响应内容反序列化为指定类型（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="options">JSON 序列化选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [Obsolete("此方法在AOT环境中不安全，请使用带有JsonTypeInfo参数的GetFromJsonAsync方法")]
        public static Task<T?> GetFromJsonAsync<T>(
            this IHttpRequestManager manager,
            string requestUri,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            return GetFromJsonAsync<T>(manager, requestUri, null, options, cancellationToken);
        }

        #endregion

        #region PostAsJsonAsync - 兼容性方法

        /// <summary>
        /// 以 JSON 格式发送 HTTP POST 请求并返回响应（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            CancellationToken cancellationToken = default)
        {
            return await PostAsJsonAsync(manager, requestUri, value, jsonTypeInfo, null, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP POST 请求并返回响应（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="options">JSON 序列化选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [Obsolete("此方法在AOT环境中不安全，请使用带有JsonTypeInfo参数的PostAsJsonAsync方法")]
        public static async Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Post, requestUri);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithJsonContent(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP POST 请求并返回响应，支持AOT
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Post, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithJsonContent(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region PutAsJsonAsync - 兼容性方法

        /// <summary>
        /// 以 JSON 格式发送 HTTP PUT 请求并返回响应（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Put, requestUri);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithJsonContent(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PUT 请求并返回响应（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="options">JSON 序列化选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Put, requestUri);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithJsonContent(json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PUT 请求并返回响应，支持AOT
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PutAsJsonWithConfigAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Put, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            //string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithJsonContent(value, jsonTypeInfo);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region PatchAsJsonAsync - 兼容性方法

        /// <summary>
        /// 以 JSON 格式发送 HTTP PATCH 请求并返回响应（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Patch, requestUri);

            // 添加JSON内容
            //string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithJsonContent(jvalue, jsonTypeInfoson);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PATCH 请求并返回响应（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="options">JSON 序列化选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Patch, requestUri);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithJsonContent(json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PATCH 请求并返回响应，支持AOT
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PatchAsJsonWithConfigAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Patch, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            //string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithJsonContent(value, jsonTypeInfo);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region DeleteFromJsonAsync - 兼容性方法

        /// <summary>
        /// 发送 HTTP DELETE 请求并将响应内容反序列化为指定类型（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        public static async Task<T?> DeleteFromJsonAsync<T>(
            this IHttpRequestManager manager,
            string requestUri,
            JsonTypeInfo<T> jsonTypeInfo,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Delete, requestUri);
            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync(stream, jsonTypeInfo, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 发送 HTTP DELETE 请求并将响应内容反序列化为指定类型（与 HttpClient API 兼容的签名）
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="options">JSON 序列化选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<T?> DeleteFromJsonAsync<T>(
            this IHttpRequestManager manager,
            string requestUri,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Delete, requestUri);
            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            return await JsonSerializer.DeserializeAsync<T>(stream, jsonOptions, cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region GetFromJsonAsync

        /// <summary>
        /// 发送 HTTP GET 请求并将响应内容反序列化为指定类型，支持请求配置
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<T?> GetFromJsonAsync<T>(
            this IHttpRequestManager manager,
            string requestUri,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Get, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();

            return await JsonSerializer.DeserializeAsync<T>(stream, jsonOptions, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 发送 HTTP GET 请求并将响应内容反序列化为指定类型，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        public static async Task<T?> GetFromJsonAsync<T>(
            this IHttpRequestManager manager,
            string requestUri,
            JsonTypeInfo<T> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Get, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync(stream, jsonTypeInfo, cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region PostAsJsonAsync

        /// <summary>
        /// 以 JSON 格式发送 HTTP POST 请求并返回响应，支持请求配置
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Post, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithJsonContent(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP POST 请求并返回响应，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PostAsJsonWithConfigAsync<TValue>(
            this IHttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Post, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithBody(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP POST 请求并将响应内容反序列化为指定类型，支持请求配置
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <typeparam name="TResult">要反序列化的结果类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<TResult?> PostAsJsonAsync<TValue, TResult>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Post, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithJsonContent(json, BodyType.Json);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync<TResult>(stream, jsonOptions, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP POST 请求并将响应内容反序列化为指定类型，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <typeparam name="TResult">要反序列化的结果类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="requestTypeInfo">请求体的 JSON 类型信息</param>
        /// <param name="responseTypeInfo">响应的 JSON 类型信息</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        public static async Task<TResult?> PostAsJsonAsync<TValue, TResult>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> requestTypeInfo,
            JsonTypeInfo<TResult> responseTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(requestTypeInfo);
            ArgumentNullException.ThrowIfNull(responseTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Post, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, requestTypeInfo);
            builder.WithJsonContent(json, BodyType.Json);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync(stream, responseTypeInfo, cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region PutAsJsonAsync

        /// <summary>
        /// 以 JSON 格式发送 HTTP PUT 请求并返回响应，支持请求配置
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Put, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithJsonContent(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PUT 请求并返回响应，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Put, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithBody(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PUT 请求并将响应内容反序列化为指定类型，支持请求配置
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <typeparam name="TResult">要反序列化的结果类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<TResult?> PutAsJsonAsync<TValue, TResult>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Put, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithBody(json, BodyType.Json);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync<TResult>(stream, jsonOptions, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PUT 请求并将响应内容反序列化为指定类型，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <typeparam name="TResult">要反序列化的结果类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="requestTypeInfo">请求体的 JSON 类型信息</param>
        /// <param name="responseTypeInfo">响应的 JSON 类型信息</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        public static async Task<TResult?> PutAsJsonAsync<TValue, TResult>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> requestTypeInfo,
            JsonTypeInfo<TResult> responseTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(requestTypeInfo);
            ArgumentNullException.ThrowIfNull(responseTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Put, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, requestTypeInfo);
            builder.WithJsonContent(json, BodyType.Json);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync(stream, responseTypeInfo, cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region PatchAsJsonAsync

        /// <summary>
        /// 以 JSON 格式发送 HTTP PATCH 请求并返回响应，支持请求配置
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Patch, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithJsonContent(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PATCH 请求并返回响应，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>HTTP 响应消息</returns>
        public static async Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Patch, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, jsonTypeInfo);
            builder.WithBody(json, BodyType.Json);

            return await builder.SendAsync(cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PATCH 请求并将响应内容反序列化为指定类型，支持请求配置
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <typeparam name="TResult">要反序列化的结果类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<TResult?> PatchAsJsonAsync<TValue, TResult>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Patch, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            string json = JsonSerializer.Serialize(value, jsonOptions);
            builder.WithBody(json, BodyType.Json);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync<TResult>(stream, jsonOptions, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 以 JSON 格式发送 HTTP PATCH 请求并将响应内容反序列化为指定类型，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="TValue">要序列化的请求体类型</typeparam>
        /// <typeparam name="TResult">要反序列化的结果类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="value">要序列化为 JSON 的值</param>
        /// <param name="requestTypeInfo">请求体的 JSON 类型信息</param>
        /// <param name="responseTypeInfo">响应的 JSON 类型信息</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        public static async Task<TResult?> PatchAsJsonAsync<TValue, TResult>(
            this HttpRequestManager manager,
            string requestUri,
            TValue value,
            JsonTypeInfo<TValue> requestTypeInfo,
            JsonTypeInfo<TResult> responseTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(requestTypeInfo);
            ArgumentNullException.ThrowIfNull(responseTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Patch, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 添加JSON内容
            string json = JsonSerializer.Serialize(value, requestTypeInfo);
            builder.WithBody(json, BodyType.Json);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync(stream, responseTypeInfo, cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region DeleteFromJsonAsync

        /// <summary>
        /// 发送 HTTP DELETE 请求并将响应内容反序列化为指定类型，支持请求配置
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项，为 null 时使用默认选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static async Task<T?> DeleteFromJsonAsync<T>(
            this HttpRequestManager manager,
            string requestUri,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(HttpMethod.Delete, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var jsonOptions = options ?? JsonSerializerFactory.GetOptions();
            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync<T>(stream, jsonOptions, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// 发送 HTTP DELETE 请求并将响应内容反序列化为指定类型，支持请求配置（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="T">要反序列化的目标类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>反序列化后的对象，如果响应内容为空则为默认值</returns>
        public static async Task<T?> DeleteFromJsonAsync<T>(
            this HttpRequestManager manager,
            string requestUri,
            JsonTypeInfo<T> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(HttpMethod.Delete, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            using var response = await builder.SendAsync(cancellationToken).ConfigureAwait(false);
            response.EnsureSuccessStatusCode();

            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
            return await JsonSerializer.DeserializeAsync(stream, jsonTypeInfo, cancellationToken).ConfigureAwait(false);
        }

        #endregion

        #region GetFromJsonAsAsyncEnumerable

        /// <summary>
        /// 获取流式 JSON 响应，并将其作为异步枚举器返回（适用于 JSON 序列或 JSON 数组）
        /// </summary>
        /// <typeparam name="T">元素类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="method">HTTP 方法</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="options">JSON 序列化选项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>异步枚举器，包含反序列化的元素</returns>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        public static IAsyncEnumerable<T> GetFromJsonAsAsyncEnumerable<T>(
            this HttpRequestManager manager,
            HttpMethod method,
            string requestUri,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            JsonSerializerOptions? options = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentNullException.ThrowIfNull(method);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);

            var builder = manager.CreateRequest(method, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            // 使用现有的流式 API
            var deserializer = JsonSerializerFactory.CreateDeserializer<T>(null, options);
            return manager.GetStreamingAsync(
                method,
                requestUri,
                deserializer,
                jsonOptions: options,
                cancellationToken: cancellationToken);
        }

        /// <summary>
        /// 获取流式 JSON 响应，并将其作为异步枚举器返回（适用于 JSON 序列或 JSON 数组）（AOT 兼容版本）
        /// </summary>
        /// <typeparam name="T">元素类型</typeparam>
        /// <param name="manager">HTTP 请求管理器</param>
        /// <param name="method">HTTP 方法</param>
        /// <param name="requestUri">请求 URI</param>
        /// <param name="jsonTypeInfo">JSON 类型信息，用于 AOT 环境</param>
        /// <param name="configureRequest">配置请求的委托</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>异步枚举器，包含反序列化的元素</returns>
        public static IAsyncEnumerable<T> GetFromJsonAsAsyncEnumerable<T>(
            this HttpRequestManager manager,
            HttpMethod method,
            string requestUri,
            JsonTypeInfo<T> jsonTypeInfo,
            Action<Core.HttpRequestBuilder>? configureRequest = null,
            CancellationToken cancellationToken = default)
        {
            ArgumentNullException.ThrowIfNull(manager);
            ArgumentNullException.ThrowIfNull(method);
            ArgumentException.ThrowIfNullOrEmpty(requestUri);
            ArgumentNullException.ThrowIfNull(jsonTypeInfo);

            var builder = manager.CreateRequest(method, requestUri);

            // 允许调用者配置请求
            configureRequest?.Invoke(builder);

            return manager.GetStreamingAsync(
                method,
                requestUri,
                jsonTypeInfo: jsonTypeInfo,
                cancellationToken: cancellationToken);
        }

        #endregion
    }
}
